package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AChronicle;
import com.ruoyi.system.service.IAChronicleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 历史记录Controller
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/dianshang/chronicle")
public class AChronicleController extends BaseController
{
    @Autowired
    private IAChronicleService aChronicleService;

    /**
     * 查询历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(AChronicle aChronicle)
    {
        startPage();
        List<AChronicle> list = aChronicleService.selectAChronicleList(aChronicle);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:list')")
    @GetMapping("/listcat")
    public TableDataInfo listcat(Long userId)
    {
        List<String> list = aChronicleService.cat(userId);
        return getDataTable(list.subList(0, Math.min(10, list.size())));
    }

    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:list')")
    @GetMapping("/listhot")
    public TableDataInfo listhot()
    {
        List<String> list = aChronicleService.hot();
        return getDataTable(list);
    }


    /**
     * 导出历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:export')")
    @Log(title = "历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AChronicle aChronicle)
    {
        List<AChronicle> list = aChronicleService.selectAChronicleList(aChronicle);
        ExcelUtil<AChronicle> util = new ExcelUtil<AChronicle>(AChronicle.class);
        util.exportExcel(response, list, "历史记录数据");
    }

    /**
     * 获取历史记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aChronicleService.selectAChronicleById(id));
    }

    /**
     * 新增历史记录
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:add')")
    @Log(title = "历史记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AChronicle aChronicle)
    {
        if(aChronicle.getCreat()==null){
            aChronicle.setCreat(new Date());
        }
        return toAjax(aChronicleService.insertAChronicle(aChronicle));
    }

    /**
     * 修改历史记录
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:edit')")
    @Log(title = "历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AChronicle aChronicle)
    {
        return toAjax(aChronicleService.updateAChronicle(aChronicle));
    }

    /**
     * 删除历史记录
     */
    @PreAuthorize("@ss.hasPermi('dianshang:chronicle:remove')")
    @Log(title = "历史记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aChronicleService.deleteAChronicleByIds(ids));
    }
}
